#coding=utf-8
#Boa:Dialog:DlgProMulUnitAdjust

import wx
import wx.grid

import kit
from product import CDataProduct

def create(parent):
    return DlgProMulUnitAdjust(parent)

[wxID_DLGPROMULUNITADJUST, wxID_DLGPROMULUNITADJUSTBTNCANCEL, 
 wxID_DLGPROMULUNITADJUSTBTNCONFIRM, wxID_DLGPROMULUNITADJUSTGRID, 
] = [wx.NewId() for _init_ctrls in range(4)]

class DlgProMulUnitAdjust(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGPROMULUNITADJUST,
              name=u'DlgProMulUnitAdjust', parent=prnt, pos=wx.Point(472, 287),
              size=wx.Size(439, 250), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u591a\u5355\u4f4d\u5546\u54c1\u5e93\u5b58\u8c03\u6574')
        self.SetClientSize(wx.Size(431, 216))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.grid = wx.grid.Grid(id=wxID_DLGPROMULUNITADJUSTGRID, name=u'grid',
              parent=self, pos=wx.Point(8, 8), size=wx.Size(416, 168), style=0)
        self.grid.SetColLabelSize(32)
        self.grid.SetRowLabelSize(20)

        self.btnConfirm = wx.Button(id=wxID_DLGPROMULUNITADJUSTBTNCONFIRM,
              label=u'\u786e\u5b9a', name=u'btnConfirm', parent=self,
              pos=wx.Point(80, 184), size=wx.Size(86, 29), style=0)
        self.btnConfirm.Bind(wx.EVT_BUTTON, self.OnBtnConfirmButton,
              id=wxID_DLGPROMULUNITADJUSTBTNCONFIRM)

        self.btnCancel = wx.Button(id=wxID_DLGPROMULUNITADJUSTBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(240, 184), size=wx.Size(86, 29), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGPROMULUNITADJUSTBTNCANCEL)

    def __init__(self, parent, inBound=False):
        self._init_ctrls(parent)
        
        self._init_grid()
        
        self.inBound = inBound
        
    def _init_grid(self):
        titles = [(u'编号', 0),
                    (u'产品', 160), 
                    (u'单位', 50), 
                    (u'数量', 70)]
        [self.COL_PROID , self.COL_PRONAME, self.COL_UNIT, 
        self.COL_CNT] = [i for i in range(len(titles))]

        self.grid.CreateGrid(0, len(titles))
        for i in range(len(titles)):
            self.grid.SetColLabelValue(i, titles[i][0])
            self.grid.SetColSize(i, titles[i][1])
        self.grid.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        
    def appendData(self, dataList):
        grid = self.grid
        for i in range(len(dataList)):
            row = grid.GetNumberRows()
            grid.AppendRows()
            for col in range(len(dataList[0])):
                grid.SetCellValue(row, col, kit.myStr(dataList[i][col]))

    
    def OnBtnConfirmButton(self, event):
        for i in range(self.grid.GetNumberRows()):
            proId = int(self.grid.GetCellValue(i, self.COL_PROID))
            unit = self.grid.GetCellValue(i, self.COL_UNIT)
            num = float(self.grid.GetCellValue(i, self.COL_CNT))
            if not self.inBound:
                num = -num
            CDataProduct.updateOnhand(proId, unit, num)
            
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
